package cn.xkai.exercise.a;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description: 杨辉三角
 * 自己的思路：直接根据杨辉三角的定义，通过深度+长度循环，每层头尾位置直接赋值1，中间根据上层相邻数据相加
 * 借鉴的思路：同理
 * @author: kaixiang
 * @date: 2022-06-28
 **/
public class Solution18 {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> list = new ArrayList<>();
        list.add(Collections.singletonList(1));
        for (int i = 1; i < numRows; i++) {
            List<Integer> subList = new ArrayList<>();
            for (int j = 0; j < i + 1; j++) {
                if (j == 0 || j == i) {
                    subList.add(1);
                    continue;
                }
                List<Integer> temp = list.get(i - 1);
                subList.add(temp.get(j - 1) + temp.get(j));
            }
            list.add(subList);
        }
        return list;
    }

    public static void main(String[] args) {
        int numRows = 5;
        Solution18 solution18 = new Solution18();
        System.out.println(new Gson().toJson(solution18.generate(numRows)));
    }
}
